
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ip_check_demo/ipChongTuCheck/MyGridItem.dart';

/**
 * 展示ip占用情况页面
 */
class IpShowingPage extends StatefulWidget {

  String Startip = '';
  String endip = '';

  IpShowingPage(this.Startip,this.endip, {super.key});

  @override
  State<StatefulWidget> createState() => _LoginPageState(this.Startip,this.endip);
}

class _LoginPageState extends State<IpShowingPage> {
  String startIp = '';
  String endIp = '';

  List<Widget> viewItems = [] ;
  late final data  = <String>[];
  late final isConnectStatus = <bool>[];

  _LoginPageState(String mStartip, String mEndip){
    startIp = mStartip;
    endIp = mEndip;
  }

  @override
  void initState() {
    initConnectPool();

    super.initState();

  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('Ip'),
      ),
      body: Container(
        padding: const EdgeInsets.all(2),
        child: GridView.count(
          crossAxisCount: 3,
          mainAxisSpacing: 2,
          crossAxisSpacing: 2,
          childAspectRatio: 1 / 0.5,
          key: const Key('GridView'),
          // children: initData().map((ipStr) => _buildItem(ipStr)).toList(),
          children: viewItems,
        ),
      ),
    );
  }


  List<String> initData(){
    List<String> list = <String>[];
    String start = (startIp.split('.')[3] ) ;
    String end = (endIp.split('.')[3]  );

    int length = int.parse(end) -  int.parse(start);

    List<String> strList =  startIp.split('.');
    String head = '${strList[0]}.${strList[1]}.${strList[2]}';

    for(int i = 0; i <= length ; i++ ){
      String finalStr = '$head.${int.parse(start) + i}';
      list.add(finalStr);
    }
    return list;
  }

  String colorString(Color color) =>
      "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";


  void initConnectPool() async {

    print('initConnectPool');
    var data =  initData();

    print(data);

    for(int i = 0 ; i < data.length ; i++){
      viewItems.add(MyGridItem(Colors.grey, data[i]));
    }

    if(mounted){
      setState(() {
      });
    }
  }

  
}